package com.keqiang.chart.impl.axis.impl.pseudo3d.bar

import android.graphics.Canvas
import android.graphics.Paint
import android.text.TextPaint
import com.keqiang.chart.impl.axis.render.YAxisRender
import com.keqiang.chart.utils.ObjectPool
import com.keqiang.chart.utils.toRadian
import kotlin.math.cos
import kotlin.math.sin

/**
 * Y轴绘制器
 *
 * @author Created by wanggaowan on 2022/7/20 09:10
 */
open class Bar3DYAxisRender(chart: BarChart3D) : YAxisRender<BarChart3D>(chart) {

    override fun onDraw(canvas: Canvas,
                        start: Float, top: Float,
                        end: Float, bottom: Float,
                        labelMaxWidth: Float,
                        gridHeight: Float,
                        xAxisRange: Float) {

        val paint = ObjectPool.obtainPaint<Paint>()
        val textPaint = ObjectPool.obtainPaint<TextPaint>(true)
        val tempRectF = ObjectPool.obtainRectF()
        val path = ObjectPool.obtainPath()

        // 绘制轴线
        val yAxis = chart.yAxis
        val xAxis = chart.xAxis
        val faceRadian = chart.faceDegrees.toRadian

        // 起始点旋转chart.faceDegrees角度后的坐标
        val xOffset = cos(faceRadian) * xAxis.lineWidth
        val x2 = start + xOffset
        val y2 = bottom - sin(faceRadian) * xAxis.lineWidth

        val x = x2 + yAxis.lineWidth / 2f
        setPaint(paint, yAxis.lineColor, yAxis.lineWidth, yAxis.linePathEffect)
        path.moveTo(x, top)
        path.lineTo(x, y2)
        canvas.drawPath(path, paint)
        xAxis.drawRectF.set(x2, top, end + xOffset, y2)

        // 绘制y轴文本
        var tempBottom = y2 - yAxis.startPadding
        textPaint.textSize = yAxis.labelTextSize
        textPaint.color = yAxis.labelColor
        setPaint(paint, yAxis.gridLineColor, yAxis.gridLineWidth, yAxis.gridLinePathEffect)

        yAxis.labelList?.let {
            for (index in it.indices) {
                if (labelMaxWidth > 0 && gridHeight > 0) {
                    if (drawLabelByGridLine) {
                        drawLabelByGridLine(canvas, textPaint, index, it, yAxis, gridHeight, x2, labelMaxWidth, tempBottom, tempRectF)
                    } else {
                        drawLabelByGridCenter(canvas, textPaint, index, it, yAxis, gridHeight, x2, labelMaxWidth, tempBottom, tempRectF)
                    }
                }

                if (index > 0 && yAxis.drawGridLine) {
                    var stopX =
                        if (yAxis.gridUseEndPadding) x2 + xAxisRange - xAxis.endPadding else x2 + xAxisRange
                    if (xAxis.drawGridLine && index == it.size - 1) {
                        stopX += xAxis.gridLineWidth / 2
                    }

                    path.reset()
                    path.moveTo(if (yAxis.gridUseStartPadding) x2 + xAxis.startPadding else x2, tempBottom)
                    path.lineTo(stopX, tempBottom)
                    canvas.drawPath(path, paint)
                }

                tempBottom -= gridHeight
            }
        }

        ObjectPool.recycleRectF(tempRectF)
        ObjectPool.recyclePaint(paint)
        ObjectPool.recyclePaint(textPaint)
        ObjectPool.recyclePath(path)
    }
}